package com.koron.css2.serviceManage.servlet;

import com.koron.css2.serviceManage.bean.BankInterfaceBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.mapper.BankInterfaceMapper;
import com.koron.css2.serviceManage.utils.CommonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bson.types.ObjectId;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;




/**
 * 银行接口 添加和编辑接口
 * @author 李攀
 *
 */
@Controller
public class BankInterfaceAction {
	private static Logger logger = LogManager.getLogger(BankInterfaceAction.class);

//	@Resource
//	public JedisPool jedisPool;
	
	@SuppressWarnings("deprecation")
	@RequestMapping(value = "/BankInterfaceAdd.htm", method = RequestMethod.POST)
	@ResponseBody
	public String BankInterfaceAdd(MultipartFile file, RequestBean requestBean, BankInterfaceBean bean) throws Exception {
		try (SessionFactory factory = new SessionFactory()) {
			// 获取用户信息(包括用户信息，用户可选水司信息，用户当前水司信息)
		//	UserInfoBean userInfo = Tools.getLoginBean(requestBean.getToken(), jedisPool);
			BankInterfaceMapper mapper = factory.getMapper(BankInterfaceMapper.class);
//			if (CommonUtils.checkIsNull(bean.getCode(), bean.getRemark())) {
//				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "缺少参数", void.class).toJson();
//			}
//			if (mapper.findCodePluginByCode(bean.getCode()).size() != 0) {
//				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "已存在与 " + bean.getCode() + " 相同编码的信息", void.class)
//						.toJson();
//			}
			
			bean.setBankInterfaceId(new ObjectId().toHexString());
			//bean.setCreateName(userInfo.getUserInfo().getName()); //TODO redis报错
			bean.setCreateTime(CommonUtils.getCurrentTime());			
			if (file != null) {
				byte[] jarFile = file.getBytes();
				bean.setJarFile(jarFile);
			}
			//bean.setStatus(1);
			mapper.insertBankInterface(bean);
			return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class).toJson();
		} catch (Exception e) {
			logger.error("非法参数", e);
			return MessageBean.create(Constant.ILLEGAL_PARAMETER, "非法参数", void.class).toJson();
		}
	}
	

	@SuppressWarnings("deprecation")
	@RequestMapping(value = "/BankInterfaceUpdate.htm", method = RequestMethod.POST)
	@ResponseBody
	public String BankInterfaceUpdate(MultipartFile file, RequestBean requestBean, BankInterfaceBean bean) throws Exception {
		if(StringUtils.isBlank(requestBean.getToken())){
			return MessageBean.create(Constant.ILLEGAL_PARAMETER, "token不能为空", void.class).toJson();
		}
		try (SessionFactory factory = new SessionFactory()) {
			BankInterfaceMapper mapper = factory.getMapper(BankInterfaceMapper.class);
			// 获取用户信息(包括用户信息，用户可选水司信息，用户当前水司信息)
			//UserInfoBean userInfo = Tools.getLoginBean(requestBean.getToken(), jedisPool);
			if (StringUtils.isBlank(bean.getBankInterfaceId())) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "主键Id不能为空", void.class).toJson();
			}
//			if (CommonUtils.checkIsNull(bean.getCode(), bean.getRemark())) {
//				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "缺少参数", void.class).toJson();
//			}
//			if (mapper.findCodePluginByCode(bean.getCode()).size() != 0 && mapper.findCodePlugin(bean).size() == 0) {
//				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "已存在与 " + bean.getCode() + " 相同编码的信息", void.class)
//						.toJson();
//			}
			
			//bean.setUpdateName(userInfo.getUserInfo().getName());
			bean.setUpdateTime(CommonUtils.getCurrentTime());
			if (file != null) {
				byte[] jarFile = file.getBytes();
				bean.setJarFile(jarFile);
			}
			mapper.updateBankInterface(bean);
			return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class).toJson();

		} catch (Exception e) {
			logger.error("非法参数", e);
			return MessageBean.create(Constant.ILLEGAL_PARAMETER, e.getMessage(), void.class).toJson();
		}
	}
	
}

